﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using MarginTrading.Services;
using MarginTrading.Entity;
using MarginTrading.Common;
using System.Data;
using System.Globalization;

namespace MarginTrading.site
{
    public partial class SellSettlementSummary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserType"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

            if (!IsPostBack)
            {
                DataTable advisor = AgentServices.GetAgentCollectionDataTable();
                DataRow dr = advisor.NewRow();
                dr["NAME"] = "All";
                dr["CODE"] = "All";
                advisor.Rows.InsertAt(dr, 0);
                DataView dv = new DataView(advisor);
                ddlAgent.DataSource = advisor;
                ddlAgent.DataTextField = "NAME";
                ddlAgent.DataValueField = "CODE";
                ddlAgent.DataBind();
            }
        }

        protected void SearchBtn_Click(object sender, EventArgs e)
        {
            DateTime date = DateTime.MinValue;
            DateTime.TryParseExact(txtDate.Text, "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
            LoadReport(ddlAgent.Text, date);
        }

        private void LoadReport(string agentCode, DateTime date)
        {
            try
            {
                Agent agent = AgentServices.GetAgentByCode(agentCode);

                if (agent == null)
                {
                    agent = new Agent();
                }
                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";
                if (agentCode != "All")
                {
                    ds.Value = ReportServices.GetSSSummaryByAgentAndDate(agentCode, date);
                }
                else
                {
                    ds.Value = ReportServices.GetSSSummaryByAgentAndDate(date);
                    agent.Name = "All";
                }

                this.ReportViewer1.LocalReport.ReportPath = "SellSettlementSummary.rdlc";
                this.ReportViewer1.LocalReport.DataSources.Clear();
                this.ReportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("Date1",date == DateTime.MinValue ? "" : date.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("AgentName",agent.Name ),
                        new ReportParameter("ReportTitle",string.Format(Reports.SETTLEMENT_SUMMARY,date.ToString(DateFormat.DATE_FORMAT)))});

                //this.reportViewer1.RefreshReport();
                //reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                //reportViewer1.ZoomMode = ZoomMode.Percent;
                //reportViewer1.ZoomPercent = 100;
            }
            catch (System.Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }

    }
}
